]> git.madduck.net Git - etc/zsh.git/blob - .zsh/zshrc/20-tempfile+dir_functions

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

do not glob for mkdir
[etc/zsh.git] / .zsh / zshrc / 20-tempfile+dir_functions
1 #
2 # Helper functions for temporary files, directories, and pastebining
3 #
4 # Copyright © 1994–2017 martin f. krafft <madduck@madduck.net>
5 # Released under the terms of the Artistic Licence 2.0
6 #
7 # Source repository: http://git.madduck.net/v/etc/zsh.git
8 #
9
10 cdt() {
11   emulate -L zsh
12   REPLY=$(mktemp -td ${1:-cdt}.XXXXXX)
13   builtin cd "$REPLY"
14   rm -f ${TMPDIR:-/tmp}/cdt.latest
15   ln -s "$REPLY" ${TMPDIR:-/tmp}/cdt.latest
16   echo "$REPLY"
17 }
18
19 _cdt() {
20   cdt "$@"
21   zle reset-prompt
22   zle -M "$REPLY"
23 }
24 zle -N _cdt
25 bindkey '\ed' _cdt
26
27 vit() {
28   emulate -L zsh
29   local prefix i
30   for i in "$@"; do
31     case "$i" in
32       -) local stdin=1; shift;;
33       *) if [ -z "${prefix:-}" ]; then
34            prefix="$i"; shift
35          else
36            zwarn "prefix $prefix already specified, skipping: $i"
37          fi
38          ;;
39     esac
40   done
41   REPLY=$(mktemp -t ${prefix:-vit}-XXXXXX.txt)
42   [ -n "$stdin" ] && cat >| "$REPLY"
43   sensible-editor +start "$REPLY" </dev/tty >/dev/tty
44   ln -sf "$REPLY" ${TMPDIR:-/tmp}/vit.latest
45   echo "$REPLY"
46 }
47 _vit() {
48   vit "$@" >/dev/null
49   zle reset-prompt
50   zle -M "$REPLY"
51 }
52 zle -N _vit
53 bindkey '\ef' _vit
54
55 pastebin() {
56   local target="${(%):-"%D{%F-%H%M%S}"}-${1##*/}"
57   pub "${1}==${target}" 2>&1
58 }
59
60 _pastebinit() {
61   emulate -L zsh
62   [[ -f "$REPLY" ]] || vit paste
63   read -q "yesno?\rShould I paste the file $REPLY? [yN] "
64   zle reset-prompt
65   if [[ ${yesno:-n} == y ]]; then
66     zle -cR "pasting $REPLY …"
67     REPLY=$(pastebin "${REPLY}")
68     zle -M "$REPLY"
69   fi
70 }
71 zle -N _pastebinit
72 bindkey '\ep' _pastebinit
73
74 _copy_reply() {
75   if [[ -n "$REPLY" ]]; then
76     if (( $+commands[xclip] )); then
77       echo -En "$REPLY" | xclip -in
78       zle -M "Copied to primary clipboard: $REPLY"
79     else
80       zle -M "Cannot copy to clipboard, xclip command not found"
81     fi
82   else
83     zle -M "Nothing to copy, \$REPLY is empty"
84   fi
85 }
86 zle -N _copy_reply
87 bindkey '\ec' _copy_reply
88
89 TS() {
90   local topic ts ret script
91   topic="${(j:_:)@}"
92   ts=${(%):-%D{%Y-%m-%d-%H%M}}
93   script="${TMPDIR:-/tmp}/$ts.$$.${topic:-$LOGNAME}.typescript"
94   PS1="
95 %# " RPS1= script -qe -c "zsh -f" -f "$script"
96   ret=$?
97   zinfo "typescript is in $script ."
98   typeset -g REPLY
99   REPLY="$script"
100   if command -v unterm >/dev/null; then
101     REPLY="${script}.txt"
102     unterm "$script" > "$REPLY"
103   zinfo "plain text transcript is in $REPLY ."
104   fi
105   return $ret
106 }
107
108 # vim:ft=zsh